# pip install -U langchain-community faiss-cpu langchain-openai tiktoken
#pip install faiss-cpu
# 如果您需要使用没有 AVX2 优化的 FAISS 进行初始化，请取消下面一行的注释
# os.environ['FAISS_NO_AVX2'] = '1'

from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter

from config.embedding_config import get_openai_embeddings_local

loader = TextLoader("knowledge.txt", encoding="UTF-8")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1200, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

embeddings = get_openai_embeddings_local()
db = FAISS.from_documents(docs, embeddings)
#索引数量
print(db.index.ntotal)
query = "Pixar公司是做什么的?"
docs = db.similarity_search(query)
print(docs[0].page_content)
